<script setup lang="ts">
import { ref } from 'vue'
import type { BannerType } from '@/types/banner'
import { BaseUrl } from '@/utils/request'
import type { Env } from 'XrFrame/xrFrameSystem'
const props = defineProps({
  banner_list: {
    type: Array<BannerType>,
    default: () => [{}],
  },
})
const activeIndex = ref(0)
// UniHelper提供的类型
const handleChange: UniHelper.SwiperOnChange = (e) => {
  activeIndex.value = e.detail!.current
}
</script>

<template>
  <view class="carousel" v-if="banner_list.length > 0">
    <swiper :circular="true" :autoplay="false" :interval="3000" @change="handleChange">
      <swiper-item v-for="item in banner_list" :key="item.id">
        <!-- 该组件类似HTML中的<a>组件，但只能跳转本地页面。目标页面必须在pages.json中注册。 -->
        <navigator url="/pages/index/index" hover-class="none" class="navigator">
          <!-- mode="aspectFill" 是一个用于图片展示的 CSS 属性，它的作用是保持图片的宽高比 -->
          <image mode="aspectFill" class="image" :src="BaseUrl + item.url"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 指示点 -->
    <view class="indicator">
      <text
        v-for="(item, index) in banner_list"
        :key="item.id"
        class="dot"
        :class="{ active: index === activeIndex }"
      ></text>
    </view>
  </view>
</template>

<style lang="scss">
:host {
  display: block;
  height: 280rpx;
}
/* 轮播图 */
.carousel {
  height: 100%;
  position: relative;
  overflow: hidden;
  transform: translateY(0);
  background-color: #efefef;
  .indicator {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 16rpx;
    display: flex;
    justify-content: center;
    .dot {
      width: 30rpx;
      height: 6rpx;
      margin: 0 8rpx;
      border-radius: 6rpx;
      background-color: rgba(255, 255, 255, 0.4);
    }
    .active {
      background-color: #fff;
    }
  }
  .navigator,
  .image {
    width: 100%;
    height: 100%;
  }
}
</style>
